# Methods added to this helper will be available to all templates in the application.
require "pp"

module ApplicationHelper

  #=====================================
  #       计算倾向测试的结果
  #=====================================
  def self.trend_test_result(array)
    hash = Hash.new
    for a in array
      count = 0
      for b in array
        if a[0] == b[0] then
          count += b[1]
        end
      end
      hash[a[0]] = count
    end
    hash.sort {|a, b| b[1] <=> a[1]}
  end
  
  
  #验证中文化
  def error_messages_for(object_name, options = {})
    options = options.symbolize_keys
    object = instance_variable_get("@#{object_name}")
    unless object.errors.empty?
      error_lis = []
      object.errors.each{ |key, msg| error_lis << content_tag("li", msg) }
      content_tag("div", content_tag(options[:header_tag] || "h2",
          "发生#{object.errors.count}个错误" ) + content_tag("ul", error_lis),
          "id" => options[:id] || "errorExplanation",
          "class" => options[:class] || "errorExplanation" )
    end
  end

  #验证中文化
  def error_messages_for111(object_name, options = {})
    options = options.symbolize_keys
    object = instance_variable_get("@#{object_name}")
    unless object.errors.empty?
      error_lis = []
      object.errors.each{ |key, msg| error_lis << content_tag("li", msg) }
      content_tag("div", content_tag("ul", error_lis),
          "id" => options[:id] || "errorExplanation",
          "class" => options[:class] || "errorExplanation" )
    end
  end


  #年龄选择器
  def age_select(model, column)
    array = []
    #array << [NOT_IMPORT, NOT_IMPORT]
    for i in 1..100  
      array << [i, i]
    end
    select(model, column, array )
  end

  #安全地得到用户的信息
  def safe_user_info
    current_user_id = session[CURRENT_USER_ID]
    user = User.find(current_user_id)
    user.password = ""
    user
  end



  #覆盖系统的方法，支持把错误信息放到<span></span>中
  def error_message_on111(tag, object, method, *args)
    options = args.extract_options!
    unless args.empty?
      ActiveSupport::Deprecation.warn('error_message_on takes an option hash instead of separate ' +
        'prepend_text, append_text, and css_class arguments', caller)

      options[:prepend_text] = args[0] || ''
      options[:append_text] = args[1] || ''
      options[:css_class] = args[2] || 'formError'
    end
    options.reverse_merge!(:prepend_text => '', :append_text => '', :css_class => 'formError')

    if (obj = (object.respond_to?(:errors) ? object : instance_variable_get("@#{object}"))) &&
      (errors = obj.errors.on(method))
      content_tag(tag,
        "#{options[:prepend_text]}#{ERB::Util.html_escape(errors.is_a?(Array) ? errors.first : errors)}#{options[:append_text]}",
        :class => options[:css_class]
      )
    else
      ''
    end
  end
  
  def current_user
    if session[CURRENT_USER_ID]
      @user = User.find(session[CURRENT_USER_ID])
      @user.password = ''
    end
    @user
  end

  def current_user_name
    if session[CURRENT_USER_ID]
      @user = User.find(session[CURRENT_USER_ID])
      (@user and @user.name) ? @user.name : '心嘉园'
    else
      '心嘉园'
    end
  end
  
  def count_by_formula(number, formula)
    #如： count_by_formual(2, "*5") , return => 2*5=10
    eval "#{number}.#{formula[0,1]} #{formula[1, formula.length]}"
  end
  
  
  def find_expl(dimen_id,marks)
    dimen=Dimen.find_by_id(dimen_id)
    explanations=dimen.explanations
    result=nil
    explanations.each do |expl|
      if  marks.to_i >= expl.start_num && marks.to_i < expl.end_num 
        result = expl.description
      end
    end 
    result
  end
  
  
    def surveys_authorith(id)
     all_ids=Survey.find_by_sql("select t1.id from surveys t1, orgs_surveys t2 where t1.id = t2.survey_id and t2.org_id = #{session[CURRENT_USER_ID]} ")
    surveyids=[]
    all_ids.each do |s|
     surveyids << s.id  
    end
     if surveyids.include?(id)
        return true        
     else
      return false
     end
   end

  
end


